/* 
 * Copyright 2001-2014 Aspose Pty Ltd. All Rights Reserved.
 *
 * This file is part of Aspose.Words. The source code in this file
 * is only intended as a supplement to the documentation, and is provided
 * "as is", without warranty of any kind, either expressed or implied.
 */

package com.aspose.words.examples.quickstart;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.examples.Utils;

public class HelloWorld {
	// 获取img标签正则
	private static final String IMGURL_REG = "/<img.*src=(.*?)[^>]*?>/";
	// 获取src路径的正则
	private static final String IMGSRC_REG = "http:\"?(.*?)(\"|>|\\s+)";

	public static void main(String[] args) throws Exception {
		String text = "已知a、b、c是<img src='http://ila.starc.com.cn:1057/image/img_mjjyw8A11c0109_3/c7c8b49e-e642-42df-a2a9-d416668a8a08.png' "
				+ "style='width:136.48px;height: 24.74px'/>\r\t△ABC的三边，"
				+ "且满足<img src='http://ila.starc.com.cn:1057/image/img_mjjyw8A11c0109_3/c7c8b49e-e642-42df-a2a9-d416668a8a08.png' "
				+ "style='width:136.48px;height: 24.74px'/>，则第三边<img src='http://ila.starc.com.cn:1057/image/img_mjjyw8A11c0109_3/c7c8b49e-e642-42df-a2a9-d416668a8a08.png' "
				+ "style='width:136.48px;height: 24.74px'/>c的取值范围是_____.<br/>";
		String imgPath = "D:\\las\\image\\onepiece.jpg";
		String dataDir = Utils.getDataDir(HelloWorld.class);
		//获取图片标签  
        List<String> imgUrl = getImageUrl(text);  
        //获取图片src地址  
        List<String> imgSrc = getImageSrc(imgUrl);  
		// new一个URL对象
		URL url = new URL(
				"http://ila.starc.com.cn:1057/image/img_mjjyw8A11c0109_3/c7c8b49e-e642-42df-a2a9-d416668a8a08.png");
		// 打开链接
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		// 设置请求方式为"GET"
		conn.setRequestMethod("GET");
		// 超时响应时间为5秒
		conn.setConnectTimeout(5 * 1000);
		// 通过输入流获取图片数据
		InputStream inStream = conn.getInputStream();
		Document doc = new Document();
		DocumentBuilder builder = new DocumentBuilder(doc);
		builder.insertImage(imgPath);
		double width = 336;
		double height = 145;
		builder.insertImage(inStream, width, height);
		builder.writeln(text);
		doc.save(dataDir + "HelloWorld_out_.docx");
		System.out.println("New Word document created successfully.");
		System.out.println(text.length());
		String[] split = text.split(IMGURL_REG);
		for (String str : imgSrc) {
			System.out.println(str);
		}
	}
	/*** 
     * 获取ImageUrl地址 
     *  
     * @param HTML 
     * @return 
     */  
    public static List<String> getImageUrl(String HTML) {  
        Matcher matcher = Pattern.compile(IMGURL_REG).matcher(HTML);  
        List<String> listImgUrl = new ArrayList<String>();  
        while (matcher.find()) {  
            listImgUrl.add(matcher.group());  
        }  
        return listImgUrl;  
    } 
    /*** 
     * 获取ImageSrc地址 
     *  
     * @param listImageUrl 
     * @return 
     */  
    public static List<String> getImageSrc(List<String> listImageUrl) {  
        List<String> listImgSrc = new ArrayList<String>();  
        for (String image : listImageUrl) {  
            Matcher matcher = Pattern.compile(IMGSRC_REG).matcher(image);  
            while (matcher.find()) {  
                listImgSrc.add(matcher.group().substring(0, matcher.group().length() - 1));  
            }  
        }  
        return listImgSrc;  
    }  
}